/*
 * @Description: 
 * @Author: mxy
 * @Date: 2022-11-17 11:40:03
 * @LastEditors: mxy
 * @LastEditTime: 2022-11-18 21:02:36
 */
axios.get('/dashboard').then(({ data }) => {
    const { data: { overview, year, salaryData, groupData, provinceData } } = data

    // console.log(overview)
    for (let k in overview) {
        document.querySelector(`[name=${k}]`).innerHTML = overview[k]
    }
    lineChart(year)
    classSalaryChart(salaryData)
    sexSalaryChart(salaryData)
    groupSalaryChart(groupData)
    mapChart(provinceData)
})
//-------------------------1、折线图---------------------------
const lineChart = (data) => {
    // console.log(data)
    let myChart = echarts.init(document.querySelector('#line'))
    let option = {
        title: {
            text: '2022全学科薪资走势',
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16
            }
        },
        xAxis: {
            type: 'category',
            data: data.map(item => item.month),
            axisLabel: {
                color: '#999'
            },
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed'
                }
            }

        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }

        },
        series: [
            {
                data: data.map(item => item.salary),
                type: 'line',
                smooth: true,
                lineStyle: {
                    width: 6
                },
                symbolSize: 10,
                areaStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [
                            {
                                offset: 0, color: '#499FEE' // 0% 处的颜色
                            },
                            {
                                offset: 0.8, color: 'rgba(255,255,255,0.2)'
                            },
                            {
                                offset: 1, color: 'rgba(255,255,255,0)' // 100% 处的颜色
                            }
                        ],
                        global: false // 缺省为 false
                    }
                }
            }
        ],
        tooltip: {
            trigger: 'axis'
        },
        grid: {
            top: '20 %'
        },
        color: [{
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
                offset: 0, color: '#499FEE' // 0% 处的颜色
            }, {
                offset: 1, color: '#5D75F0' // 100% 处的颜色
            }],
            global: false // 缺省为 false
        }],
    }
    myChart.setOption(option)
}


//-------------------------2、饼图-右上角---------------------------
const classSalaryChart = (data) => {
    // console.log(data)
    let myChart = echarts.init(document.querySelector('#salary'))
    let option = {
        title: {
            text: '班级薪资分布',
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16
            }
        },
        tooltip: {
            trigger: 'item'
        },
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
        legend: {
            bottom: '6%',
            left: 'center',
            itemWidth: 16
        },
        series: [
            {
                name: '班级薪资分布',
                type: 'pie',
                radius: ['50%', '64%'],
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },

                labelLine: {
                    show: false
                },
                // data: [
                //     { value: 1048, name: '1万以下' },
                //     { value: 235, name: '1万-2万' },
                //     { value: 580, name: '1.5万-2万' },
                //     { value: 484, name: '2万以上' }
                // ]
                data: data.map(item => ({ value: item.g_count + item.b_count, name: item.label }))
            }
        ]
    };
    myChart.setOption(option)
}



//-------------------------3、饼图-左下角---------------------------
const sexSalaryChart = (data) => {
    // console.log(data)
    let myChart = echarts.init(document.querySelector('#gender'))
    let option = {
        title: [
            {
                text: '男女生薪资分布',
                top: 10,
                left: 10,
                textStyle: {
                    fontSize: 16
                }
            },
            {
                text: '男生',
                top: '45%',
                left: '45%',
                textStyle: {
                    fontSize: 12
                }
            },
            {
                text: '女生',
                top: '85%',
                left: '45%',
                textStyle: {
                    fontSize: 12
                }
            }
        ],
        tooltip: {
            trigger: 'item'
        },
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
        series: [
            {
                name: '男生薪资分布',
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '30%'],

                // data: [
                //     { value: 15, name: '1万以下' },
                //     { value: 235, name: '1万-2万' },
                //     { value: 580, name: '1.5万-2万' }
                // ]
                data: data.map(item => ({ value: item.b_count, name: item.label }))
            },
            {
                name: '女生薪资分布',
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '70%'],

                data: data.map(item => ({ value: item.g_count, name: item.label }))
            }
        ]
    };
    myChart.setOption(option)
}


//-------------------------4、柱状图---------------------------
const groupSalaryChart = (data) => {
    // console.log(data);
    const myChart = echarts.init(document.querySelector('#lines'))
    const option = {
        xAxis: {
            type: 'category',
            data: data[1].map(item => item.name),
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dotted'
                }
            },
            axisLabel: {
                color: '#999'
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dotted'
                }
            }
        },
        series: [
            {
                data: data[1].map(item => item.hope_salary),
                type: 'bar',
                name: '期望薪资'
            },
            {
                data: data[1].map(item => item.salary),
                type: 'bar',
                name: '就业薪资'
            }
        ],
        grid: {
            left: 70,
            top: 30,
            right: 30,
            bottom: 50
        },
        tooltip: {},
        color: [
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#34D39A' // 0% 处的颜色
                }, {
                    offset: 1, color: 'rgba(52,211,154,0.2)' // 100% 处的颜色
                }],
                global: false // 缺省为 false
            },
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#499FEE' // 0% 处的颜色
                }, {
                    offset: 1, color: 'rgba(73,159,238,0.2)' // 100% 处的颜色
                }],
                global: false // 缺省为 false
            },
        ]

    }
    myChart.setOption(option)

    document.querySelector('#btns').addEventListener('click', function (e) {
        if (e.target.tagName === 'BUTTON') {
            document.querySelector('#btns .btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
            const index = e.target.innerHTML
            option.xAxis.data = data[index].map(item => item.name)
            option.series[0].data = data[index].map(item => item.hope_salary)
            option.series[1].data = data[index].map(item => item.salary)
            myChart.setOption(option)
        }
    })
}

//-------------------------5、地图-----------------------------
const mapChart = (data) => {
    console.log(data)
    const mapData = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 3 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 }
    ]
    mapData.forEach(item => {
        data.find(i => {
            if (i.name.includes(item.name)) {
                item.value = i.value
            }
        })
    })
    let myChart = echarts.init(document.querySelector('#map'))
    let option = {
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16
            }
        },
        series: [{
            type: 'map',
            map: 'china',
            name: '籍贯分布',
            data: mapData,
            label: {
                show: 'true',
                fontSize: 10,
                color: 'rgba(0,0,0,0.7)'
            },
            itemStyle: {
                borderColor: 'rgba(0, 0, 0, 0.2)',
                areaColor: '#E0FFFF'
            },
            emphasis: {
                itemStyle: {
                    areaColor: '#34D39A',
                    borderWidth: 0,
                    shadowBlur: 20,
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                },
            }
        }],
        tooltip: {
            formatter: '{b}: {c}位学员',
            textStyle: {
                color: '#fff'
            },
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,.5)'
        },
        visualMap: {
            type: 'continuous',
            min: 0,
            max: 6,
            inRange: {
                color: ['#fff', '#0075F0'],
            },
            left: 40,
            bottom: 20,
            text: [6, 0]
        }
    }
    myChart.setOption(option)
}

